package com.ffapp.demo.chat;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ffapp.demo.R;
import com.ffapp.demo.basis.BasisActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AdapterMore extends RecyclerView.Adapter<AdapterMore.ViewHolder> {


        private ArrayList<MoreGridData> mListBean;
        BasisActivity mContext;
    MoreItemClickListener mListener;

        public AdapterMore(BasisActivity context, ArrayList<MoreGridData> listBean, MoreItemClickListener listener) {
            mContext = context;
            mListBean = listBean;
            mListener = listener;
        }


//    public void update(ChatMsgListBean listBean){
//            if(mListBean == null || mListBean.getList().size() == listBean.getList().size()){
//                mListBean = listBean;
//                notifyDataSetChanged();
//            }else {
//                int itemChange = listBean.getList().size() - mListBean.getList().size();
//                notifyItemInserted();
//            }
//    }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_more_grid_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.img_icon)
            public ImageView img_icon;


            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position) {
                MoreGridData data = mListBean.get(position);
                text_name.setText(data.getName());
                img_icon.setImageResource(data.getImg_res());
                list_item.setTag(position);
            }

            @OnClick(R.id.list_item)
            void onItemClick(){
                int position = (int) list_item.getTag();
                MoreGridData data = mListBean.get(position);
                if(mListener != null){
                    mListener.onItemClick(data);
                }
            }

        }

    public interface MoreItemClickListener {
        abstract void onItemClick(MoreGridData data);
    }

        public static class MoreGridData{
            private String name;
            private int img_res;

            public MoreGridData(String name, int img_res) {
                this.name = name;
                this.img_res = img_res;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public int getImg_res() {
                return img_res;
            }

            public void setImg_res(int img_res) {
                this.img_res = img_res;
            }
        }
    }